package com.cy.aircompressor.main.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.core.content.ContextCompat;

import com.cy.aircompressor.R;
import com.cy.aircompressor.databinding.HeaderBarLayoutBinding;
import com.cy.aircompressor.databinding.TabBarLayoutBinding;


/**
 * Created by YoKeyword on 16/6/3.
 */
public class HeaderBarTab extends FrameLayout {
    private TextView mTvTitle;
    private Context mContext;
    private int mTabPosition = -1;


    public HeaderBarTab(Context context, CharSequence title) {
        this(context, null, title);
    }

    public HeaderBarTab(Context context, AttributeSet attrs, CharSequence title) {
        this(context, attrs, 0, title);
    }

    public HeaderBarTab(Context context, AttributeSet attrs, int defStyleAttr, CharSequence title) {
        super(context, attrs, defStyleAttr);
        initView(context, title);
    }

    private void initView(Context context, CharSequence title) {
        mContext = context;
        TypedArray typedArray = context.obtainStyledAttributes(new int[]{R.attr.selectableItemBackgroundBorderless});
        Drawable drawable = typedArray.getDrawable(0);
        setBackgroundDrawable(drawable);
        typedArray.recycle();

        LayoutInflater mInflater = LayoutInflater.from(context);
        HeaderBarLayoutBinding mBinding = HeaderBarLayoutBinding.inflate(mInflater);

        mTvTitle = mBinding.mTvTitle;
        mTvTitle.setText(title);
        mTvTitle.setTextColor(Color.parseColor("#1FC6FF"));
        mTvTitle.setBackground(ContextCompat.getDrawable(mContext,R.drawable.btn_nav_bg_unselector));
        addView(mBinding.getRoot());
    }


    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
        if (selected) {
            mTvTitle.setTextColor(Color.parseColor("#FFFFFF"));
            mTvTitle.setBackground(ContextCompat.getDrawable(mContext,R.drawable.btn_nav_bg_selector));
        } else {
            mTvTitle.setTextColor(Color.parseColor("#1FC6FF"));
            mTvTitle.setBackground(ContextCompat.getDrawable(mContext,R.drawable.btn_nav_bg_unselector));
        }
    }

    public void setTabPosition(int position) {
        mTabPosition = position;
        if (position == 0) {
            setSelected(true);
        }
    }

    public int getTabPosition() {
        return mTabPosition;
    }


    private int dip2px(Context context, float dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
    }
}
